<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\Services;

import("Classes.Services.AbstractService");
import("Classes.Util.HTTP.HTTPRequest");

use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Factory\ObjectFactory;
use \PsiFramework\Classes\Services\AbstractService;
use \PsiFramework\Classes\Util\HTTP\HTTPRequest;
use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Util\Object\Date;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Service de listing des auteurs
 *
 * @package Services
 */
class CreateEntryService extends AbstractService
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * The main function of the method which is called every time
     * an service is created by the front controller.
     *
     * @param $request      the HTTP request to be treated by the action
     */
    public function execute(Request $request)
    {
        parent::execute($request);

        $entry = null ;

        $siteId = $request->getParameter("site_id");
        $idOnSite = $request->getParameter("id_on_site");
        $userId = $request->getParameter("user_id");

        $siteDao = DAOFactory::getDAO('AlloMediaSite');
        $site = $siteDao->retrieveByPk($siteId);

        if (!is_null($site)) {
            $url
                = $site->getUri().
                '/webservices/infos_fiche.php?fiche='.$idOnSite ;
            $httpRequest = new HTTPRequest($url);
            $response = $httpRequest->execute(HTTPRequest::$get);

            $document = new \DomDocument();
            $document->loadXML($response);
            $allomediaElement = $document->documentElement;

            $sites = $allomediaElement->getElementsByTagName("site");
            if (!is_null($sites)) {
                $siteEl = $sites->item(0);

                $fiches = $siteEl->getElementsByTagName("fiche");
                if (!is_null($fiches)) {
                    $ficheEl = $fiches->item(0);

                    if (!is_null($ficheEl)) {
                        $name = null;
                        $nameEl = $ficheEl->getElementsByTagName("nom");
                        if (!is_null($nameEl) && !is_null($nameEl->item(0))) {
                            $name = $nameEl->item(0)->nodeValue ;
                        }
                        
                        $address = null;
                        $addressEl = $ficheEl->getElementsByTagName("adresse");
                        if (!is_null($addressEl)
                            && !is_null($addressEl->item(0))
                        ) {
                            $address = $addressEl->item(0)->nodeValue ;
                        }
                        
                        $zip = null;
                        $zipEl = $ficheEl->getElementsByTagName("code_postal");
                        if (!is_null($zipEl) && !is_null($zipEl->item(0))) {
                            $zip = $zipEl->item(0)->nodeValue ;
                        }
                        
                        $city = null;
                        $cityEl = $ficheEl->getElementsByTagName("ville");
                        if (!is_null($cityEl) && !is_null($cityEl->item(0))) {
                            $city = $cityEl->item(0)->nodeValue ;
                        }
                        
                        $tel = null;
                        $telEl = $ficheEl->getElementsByTagName("tel");
                        if (!is_null($telEl) && !is_null($telEl->item(0))) {
                            $tel = $telEl->item(0)->nodeValue ;
                        }

                        $categoryName = null;
                        $catEl = $ficheEl->getElementsByTagName("categorie");
                        if (!is_null($catEl) && !is_null($catEl->item(0))) {
                            $categoryName = $catEl->item(0)->nodeValue ;
                        }

                        $siteMembershipDao
                            = DAOFactory::getDAO('SiteMembership');
                        $baseMembership
                            = $siteMembershipDao->retrieveBySiteId(
                                $site,
                                $idOnSite
                            );

                        if (is_null($baseMembership)) {
                            $entry
                                = ObjectFactory::getNewObject('AlloMediaEntry');
                        } else {
                            $entry = $baseMembership->getEntry();
                        }

                        $entry->setName($name);
                        $entry->setAddress($address);
                        $entry->setZip($zip);
                        $entry->setCity($city);
                        $entry->setTel($tel);
                        $entry->setModificationDate(Date::now());

                        $categoryDao = DAOFactory::getDAO('SiteCategory');
                        $category
                            = $categoryDao->retrieveByPartialName(
                                $categoryName,
                                true
                            );
                        $entry->setCategory($category);

                        $userDao = DAOFactory::getDAO('User');
                        $user = $userDao->retrieveByPk($userId);
                        $entry->setUser($user);

                        $entryDao = DAOFactory::getDAO('AlloMediaEntry');
                        $entryDao->save($entry);

                        $siteMembershipDao
                            = DAOFactory::getDAO('SiteMembership');
                        $membership
                            = ObjectFactory::getNewObject('SiteMembership');
                        $membership->setEntry($entry);
                        $membership->setSite($site);
                        $membership->setSiteUserId($idOnSite);

                        $baseMembership
                            = $siteMembershipDao->retrieveByPk(
                                $membership->getId()
                            );
                        if (is_null($baseMembership)) {
                            $siteMembershipDao->save($membership);
                        }
                    }
                }
            }
        }

        if (!is_null($entry)) {
        	echo $entry->getId();
        }
    }

}
